<?php

function P($obj,$isEnd=true){
	header("Content-type: text/html; charset=utf-8");
	echo '<pre style="margin:10px; padding:10px; border:1px #ccc solid; background-color:#f9f9f9; font-size:13px;">';
	print_r($obj);
	echo '</pre>';
	if($isEnd) exit();
}

/**
* 无限级分类排序 按级别归类
* @return
*/
function category($data,$pid='0'){
    $arr = array();
    foreach($data as $v){
        if($v['pid'] == $pid){
            $v['child'] = category($data,$v['id']);
            $arr[] = $v;    
        }
    }
    return $arr;
}

/**
 * 删除目录或者文件
 * @param  string  $path
 * @param  boolean $is_del_dir
 * @return fixed
 */
function del_dir_or_file($path, $is_del_dir = FALSE) {
	
    $handle = opendir($path);
    if ($handle) { // $path为目录路径 
        while (false !== ($item = readdir($handle))) { 
            if ($item != '.' && $item != '..') {  // 除去..目录和.目录
                if (is_dir("$path/$item")) { 
                    del_dir_or_file("$path/$item", $is_del_dir);   // 递归删除目录
                } else { 
					//echo "del tfile $path/$item<br />";
                    unlink("$path/$item");  // 删除文件
                }
            }
        }
        closedir($handle);
        if ($is_del_dir) { 					
			//echo "del dir $path <br />"; 
            return rmdir($path); // 删除目录
        }
    }else {
        if (file_exists($path)) {		
			//echo "del file $path <br />"; 
            return unlink($path);
        } else {
            return false;
        }
    }
}

function gtTouser(){
	vendor('Gtpush.Igetui'); 
    $igt = new \IGeTui(NULL,APPKEY,MASTERSECRET,false); 
    $template = IGtTransmissionTemplateDemo(); 
    $message = new \IGtSingleMessage(); 
    $message->set_isOffline(true);//是否离线
    $message->set_offlineExpireTime(3600*12*1000);//离线时间
    $message->set_data($template);//设置推送消息类型  
    //接收方
    $target = new \IGtTarget();
    $target->set_appId(APPID);
    $target->set_clientId(CID);  
    try {
        $rep = $igt->pushMessageToSingle($message, $target);
		P($rep);
        var_dump($rep);
        echo ("<br><br>");

    }catch(RequestException $e){ 
        $requstId =e.getRequestId();
        $rep = $igt->pushMessageToSingle($message, $target,$requstId);
        var_dump($rep);
        echo ("<br><br>");
    }
}

/**
 * 加水印
 * @param string $filename 文件路径
 * @return mixed
 */
function Watermark($filename){
	  	 $filename = '.'.$filename;
		$finfo = pathinfo($filename);
		$exe = strtolower($finfo['extension']);//后缀名
		$exe = ($exe=='jpg')?'jpeg':$exe;
		$iSize = getimagesize($filename); //获取图片信息
		$width = $iSize[0];
		$newwidth=$width/3;//设定水印比例
		$markSize = getimagesize('./logo.png');//水印
		$imgMark = imagecreatefrompng('./logo.png');
		$s = array( $newwidth, $newwidth*$markSize[1]/$markSize[0]);
		$newMark = imagecreatetruecolor($s[0],$s[1]);//生成新的水印
		
		$alpha = imagecolorallocatealpha($newMark, 0, 0, 0, 127);//附上颜色
		imagefill($newMark, 0, 0, $alpha);
		
		imagecopyresized($newMark,$imgMark,0,0,0,0,$s[0],$s[1],$markSize[0],$markSize[1]);//裁剪
		$fn = 'imagecreatefrom'.$exe;
		$simg = $fn($filename);
		imagecopy($simg,$newMark,$iSize[0]-$s[0],$iSize[1]-$s[1],0,0,$s[0],$s[1]);//加水印
		$fn2 = 'image'.$exe;
		imagesavealpha($simg, true);//开启png透明
		
		$fn2($simg,$filename);//保存图片
		
	}



//function IGtTransmissionTemplateDemo(){
//  $template =  new \IGtTransmissionTemplate();
//  $template->set_appId(APPID);//应用appid
//  $template->set_appkey(APPKEY);//应用appkey
//  $template->set_transmissionType(1);//透传消息类型
//  $template->set_transmissionContent("测试离线ddd");//透传内容 
//  //APN高级推送
//  $apn = new IGtAPNPayload();
//  $alertmsg=new DictionaryAlertMsg();
//  $alertmsg->body="body";
//  $alertmsg->actionLocKey="ActionLockey";
//  $alertmsg->locKey="LocKey";
//  $alertmsg->locArgs=array("locargs");
//  $alertmsg->launchImage="launchimage";
////        IOS8.2 支持
//  $alertmsg->title="Title";
//  $alertmsg->titleLocKey="TitleLocKey";
//  $alertmsg->titleLocArgs=array("TitleLocArg");
//
//  $apn->alertMsg=$alertmsg;
//  $apn->badge=7;
//  $apn->sound="";
//  $apn->add_customMsg("payload","payload");
//  $apn->contentAvailable=1;
//  $apn->category="ACTIONABLE";
//  $template->set_apnInfo($apn); 
//  return $template;
//}
